const app = getApp();

Component({
  properties: {
    
  },
  data: {
    hideAttr:true,
    showInput:false,
    attrs:[],
    currentIndex:''
  },

  ready: function () {
  },
  methods: {
    init:function(attrs = []){
      this.setData({
        hideAttr : false,
        attrs : attrs
      })
    },
    //调用输入框子组件，规格弹窗
    addAttr: function () {
      this.selectComponent('#inputModal').init();
    },
    //输入框子组件传递参数回来,规格
    setAttr:function(e){
      var content = e.detail.content;
      if (!content) {
        return;
      }
      this.data.attrs.push({name:content,value:[]});
      var attrs = this.data.attrs;
      this.setData({
        attrs
      })
    },
    //调用输入框子组件，规格值弹窗
    addValue: function (e) {
      this.data.currentIndex = e.currentTarget.dataset.index;
      this.selectComponent('#inputModalValue').init();
      this.setData({
        currentIndex:e.currentTarget.dataset.index
      })
    },
    //输入框子组件传递参数回来，规格值
    setValue: function (e) {
      var content = e.detail.content;
      if(!content){
        return;
      }
      this.data.attrs[this.data.currentIndex].value.push(content);
      var attrs = this.data.attrs;
      this.setData({
        attrs
      })
    },
    //删除规格
    delAttrValue: function (e) {
      var index = e.currentTarget.dataset.index;
      this.data.attrs.splice(index, 1);
      var attrs = this.data.attrs;
      this.setData({
        attrs
      })
    },
    //删除规格值
    deleteValue: function (e) {
      var parentIndex = e.currentTarget.dataset.parent_index;
      var index = e.currentTarget.dataset.index;
      this.data.attrs[parentIndex].value.splice(index, 1);
      var attrs = this.data.attrs;
      this.setData({
        attrs
      })
    },
    //保存预览
    preview:function(){
      this.triggerEvent('getAttr', { attrs: this.data.attrs , isNext:false });
      this.setData({
        hideAttr: true
      })
    },
    //保存并且下一步
    next: function () {
      this.triggerEvent('getAttr', { attrs: this.data.attrs, isNext:true });
      this.setData({
        hideAttr: true
      })
    },
  }
})